package main

import (
	"iot_gateway/handlers"
	"iot_gateway/models"
	"iot_gateway/task"
	"log"

	"github.com/gin-gonic/gin"
)

func main() {
	// 初始化广播器
	origin := models.NewBroadcastMessage()
	// 创建本地广播消息处理器
	task := task.NewLocalBroadcastMessage(origin)
	// 创建WebSocket处理器
	wsHandler := handlers.NewLocalBroadcastMessage(origin)

	// 启动设备监控任务
	go task.StartDeviceMonitor()

	// 设置WebSocket路由
	r := gin.Default()
	r.GET("/ws", func(c *gin.Context) {
		wsHandler.HandleWebSocket(c.Writer, c.Request)
	})

	r.GET("/api/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	// 模拟一个获取设备列表的接口
	r.GET("/api/devices", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"data": []gin.H{
				{"id": "DEV-001", "name": "温度传感器", "status": "online"},
			},
		})
	})
	// 启动HTTP服务器
	log.Println("WebSocket server starting on :8082")
	if err := r.Run(":8082"); err != nil {
		log.Fatal("Server failed to start: ", err)
	}
	// 在 8080 端口启动服务
	//r.Run(":8082")
}
